package com.tudouji.project.permission.service.impl;

import com.flow.service.mapper.IFlowTempMapper;
import com.flow.service.pojo.flow.FlowTemp;
import com.flow.service.process.service.IFlowTempService;
import com.flow.service.process.service.impl.FlowTempServiceImpl;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.flow.domain.Flowtemp;
import com.tudouji.project.flow.mapper.FlowtempMapper;
import com.tudouji.project.permission.domain.Permissiondef;
import com.tudouji.project.permission.domain.Salespermission;
import com.tudouji.project.permission.mapper.PermissiondefMapper;
import com.tudouji.project.permission.mapper.SalespermissionMapper;
import com.tudouji.project.permission.service.IPermissiondefService;
import com.tudouji.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限定义Service业务层处理
 *
 * @author wf
 * @date 2021-04-01
 */
@Service
public class PermissiondefServiceImpl implements IPermissiondefService
{
    @Autowired
    private PermissiondefMapper permissiondefMapper;

    @Autowired
    private SalespermissionMapper salespermissionMapper;
    @Autowired
    private FlowTempServiceImpl flowTempService;
    @Autowired
    private IFlowTempMapper flowTempMapper;

    /**
     * 查询权限定义
     *
     * @param guid 权限定义ID
     * @return 权限定义
     */
    @Override
    public Permissiondef selectPermissiondefById(String guid)
    {
        return permissiondefMapper.selectPermissiondefById(guid);
    }

    /**
     * 查询权限定义列表
     *
     * @param permissiondef 权限定义
     * @return 权限定义
     */
    @Override
    public List<Permissiondef> selectPermissiondefList(Permissiondef permissiondef)
    {
        return permissiondefMapper.selectPermissiondefList(permissiondef);
    }

    /**
     * 新增权限定义
     *
     * @param permissiondef 权限定义
     * @return 结果
     */
    @Override
    public int insertPermissiondef(Permissiondef permissiondef)
    {
        permissiondef.setGuid(IdUtils.fastSimpleUUID());
        return permissiondefMapper.insertPermissiondef(permissiondef);
    }

    /**
     * 修改权限定义
     *
     * @param permissiondef 权限定义
     * @return 结果
     */
    @Override
    public int updatePermissiondef(Permissiondef permissiondef)
    {
        return permissiondefMapper.updatePermissiondef(permissiondef);
    }

    /**
     * 批量删除权限定义
     *
     * @param guids 需要删除的权限定义ID
     * @return 结果
     */
    @Override
    public int deletePermissiondefByIds(String[] guids)
    {
        return permissiondefMapper.deletePermissiondefByIds(guids);
    }

    /**
     * 删除权限定义信息
     *
     * @param guid 权限定义ID
     * @return 结果
     */
    @Override
    public int deletePermissiondefById(String guid)
    {
        return permissiondefMapper.deletePermissiondefById(guid);
    }

    @Override
    public List<Permissiondef> getDictEmnu(Salespermission salespermission){
        return this.permissiondefMapper.getDictEmnu(salespermission);
    }

    @Override
    public AjaxResult showPrice(Long id) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        FlowTemp flowTemp = flowTempMapper.selectFlowTempById(id);
        if (StringUtils.isEmpty(flowTemp.getSensDataPerms())){
            return AjaxResult.success("0");
        }

        Salespermission salespermission = new Salespermission();
        salespermission.setUserId(sysUser.getUserId());
        salespermission.setPermscope(flowTemp.getSensDataPerms());
        List<Salespermission>  salespermissionsIn = salespermissionMapper.selectUserPermissInFlowTemp(salespermission);
        if (salespermissionsIn.size() > 0){
            return AjaxResult.success("1");
        }
        return AjaxResult.success("0");
    }

    @Override
    public AjaxResult getFuncs() {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        List<String> funcs = permissiondefMapper.getFuncs(userId);
        ArrayList<String> funcsItem = new ArrayList<>();
        if (funcs.size() > 0){
            for (int i = 0; i < funcs.size(); i++) {
                String func = funcs.get(i);
                if (StringUtils.isNotEmpty(func)){
                    String[] n = func.split(",");
                    for (int j = 0; j < n.length; j++) {
                        if (!funcsItem.contains(n[j])){
                            funcsItem.add(n[j]);
                        }
                    }
                }
            }
        }
        return AjaxResult.success(funcsItem);
    }
}
